function TeaMinderAssistant() {
	
}

TeaMinderAssistant.prototype.setup = function() {
	this.controller.listen($('start_timer'), Mojo.Event.tap, this.handelStartTimer.bind(this));
	this.controller.setupWidget(Mojo.Menu.commandMenu, undefined, teaMenuAttrRunning);
};

TeaMinderAssistant.prototype.activate = function(event) {
	var selectedTea = getCookieValue('Running');
	
	if(selectedTea.paused == false)
	{
		this.targetTime = new Date(selectedTea.targetTime);
		this.setInterval();
	}
	else
	{
		$('timer').innerHTML = this.checkTime(selectedTea.mins) + ":" + this.checkTime(selectedTea.secs);
	}
	
	$("type").innerHTML = selectedTea.type;
	$("amount").innerHTML = selectedTea.amount;
	$("strength").innerHTML = selectedTea.strength;
	
	
};


TeaMinderAssistant.prototype.deactivate = function(event) {
	clearInterval(this.interval);
};

TeaMinderAssistant.prototype.cleanup = function(event) {
	
};

TeaMinderAssistant.prototype.updateTimer = function()
{
	var selectedTea = getCookieValue('Running');
	
	var time = new Date();

	if( time > this.targetTime)
	{
		$('timer').innerHTML = "DONE!!";
		clearInterval(this.interval);
		return;
	}

	var secs = (120 + this.targetTime.getSeconds() - time.getSeconds()) % 60;	
	var mins = (120 + this.targetTime.getMinutes() - time.getMinutes()) % 60;
	if(secs > this.targetTime.getSeconds())
		mins -= 1;

	$('timer').innerHTML = ((mins <10) ? "0" + mins : mins) + ":" + ((secs < 10) ? "0"+ secs : secs);
};

TeaMinderAssistant.prototype.handelStartTimer = function(event)
{
	
	var selectedTea = getCookieValue('Running');

	var currentTime = new Date();
	this.targetTime = new Date();
	Mojo.Log.info('set time');
	this.targetTime.setMinutes(currentTime.getMinutes() + selectedTea.mins);	
	this.targetTime.setSeconds(currentTime.getSeconds() + selectedTea.secs);
	Mojo.Log.info('end set time');
	
	this.updateTimer();

	$("date").innerHTML = "Started at: " + currentTime.toLocaleTimeString(); 
	$("date").innerHTML += "<br/>Ready at: " + this.targetTime.toLocaleTimeString();
	
	var time = this.checkTime(this.targetTime.getUTCMonth()+1) + "/" +
		this.checkTime(this.targetTime.getUTCDate()) + "/" +
		this.targetTime.getUTCFullYear() +
		" " +
		this.checkTime(this.targetTime.getUTCHours()) + ":" +
		this.checkTime(this.targetTime.getUTCMinutes()) + ":" +
		this.checkTime(this.targetTime.getUTCSeconds());
	Mojo.Log.info('time: ' + time);
	this.controller.serviceRequest('palm://com.palm.power/timeout', {
		method: "set",
		parameters: {
			"wakeup": true,
			"key": "tea",
			"uri": "palm://com.palm.applicationManager/launch",
			"params": '{"id":"com.fuzzymole.teaminder","params":{"action":"setTimeout"}}',
			"at": time
			}
	});

	this.setInterval();
	
	selectedTea.targetTime = this.targetTime.toString();
	selectedTea.paused = false;	
	setCookieValue('Running', selectedTea);
	
};

TeaMinderAssistant.prototype.setInterval = function(){
	var that = this;
	this.interval = window.setInterval(function () {
		that.updateTimer();
		
	}, 1000);
};

TeaMinderAssistant.prototype.checkTime = function(number) {
	return ((number < 10) ? "0" + number : number);
};